package restful.api;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import restful.annotation.BoxPower;
import restful.bean.Result;


@Path("/box")
public class GameAPI {
	
	@Context HttpServletRequest request;
	
	@POST
	@Path("/ini")
	@Produces("application/json;charset=UTF-8")	
	public Result ini(@FormParam("username") String username) {
		HashMap<String,String> powerList = new HashMap<String,String>();
		powerList.put("box1","");
		request.getSession().setAttribute("powerList", powerList);
		request.getSession().setAttribute("username", username);		
	    return new Result(0,username+",你胆敢来挑战我？那就试试吧！","");
	}
	
	@POST
	@Path("/question1")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box1")
	public Result question1() {
		Result result = new Result(0, "登陆月球采样的机器人叫什么", "/box/answer1");
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Path("/answer1")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box1")
	public Result answer1(@FormParam("answer") String answer) {
		if (answer.equals("玉兔")) {
			HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
			powerList.put("box2", "");
			return new Result(0, "算你运气好，下次就没这么好运了！", "");
		}else {
			return new Result(-10, "你真是个衰仔！", "");
		}
				
	}
	
	@POST
	@Path("/question2")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box2")
	public Result question2() {
		Result result = new Result(0, "蒙拉丽莎微笑隐藏着什么秘密？", "/box/answer2");
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Path("/answer2")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box2")
	public Result answer2(@FormParam("answer") String answer) {
		if (answer.equals("达芬奇")) {
			HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
			powerList.put("box3", "");
			return new Result(0, "不错啊，我喜欢你。", "");
		}else {
			return new Result(-10, "宝贝，再试试", "");
		}
	}
	
	@POST
	@Path("/question3")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box3")
	public Result question3() {
		Result result = new Result(0, "造就我的工具是什么？", "/box/answer3");
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Path("/answer3")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box3")
	public Result answer3(@FormParam("answer") String answer) {
		if (answer.equals("eclipse")) {
			HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
			powerList.put("box4", "");
			return new Result(0, "5555，我的宝贝全丢了...", "");
		}else {
			return new Result(-10, "还好没打开，嘿嘿嘿！", "");
		}
	}
	
	@POST
	@Path("/question4")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box4")
	public Result question4() {
		Result result = new Result(0, "什么动物早上用四条腿走路，中午用两条腿走路，晚上用三条腿走路。", "/box/answer4");
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Path("/answer4")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box4")
	public Result answer4(@FormParam("answer") String answer) {
		if (answer.equals("人")) {
			HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
			powerList.put("box5", "");
			return new Result(0, "亲，这些东西都归你了", "");
		}else {
			return new Result(-10, "再想想，再想想", "");
		}
	}
	
	@POST
	@Path("/question5")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box5")
	public Result question5() {
		Result result = new Result(0, "阿拉丁神灯的男主人公叫什么？", "/box/answer5");
	    return result;
	}
	
	@SuppressWarnings("unchecked")
	@POST
	@Path("/answer5")
	@Produces("application/json;charset=UTF-8")
	@BoxPower("box5")
	public Result answer5(@FormParam("answer") String answer) {
		if (answer.equals("阿拉丁")) {
			HashMap<String, String> powerList = (HashMap<String, String>) request.getSession().getAttribute("powerList");
			powerList.put("box6", "");
			return new Result(10, "真倒霉，我记住你了。", "/box/finish");
		}else {
			return new Result(-10, "想打开我，没这么容易！", "");
		}
	}
	
	@POST
	@Path("/finish")	
	@Produces("application/json;charset=UTF-8")
	@BoxPower("finish")
	public Result finish() {
	   // 钥匙与钥匙孔
		return new Result(0, "好吧，你成功了，钥匙在这里，拿去吧... 也许我太老了...", "");
	}


}
